<template>
  <div>
    <h1>分类列表页</h1>
    <!-- 列表数据绑定到item -->
    <el-table :data="items">
      <el-table-column prop="_id" label="ID" width="240"></el-table-column>
      <el-table-column prop="parent" label="上级分类"></el-table-column>
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="$router.push(`/Categories/edit/${scope.row._id}`)">编辑</el-button>
          <el-button type="text,danger" size="mini"  @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  data() {
    return {
      items: [],
    };
  },
  methods: {
    async fetch() {
      const res = await this.$http.get("categories");
      this.items = res.data;
      console.log(res);
    },
    // 触发方法
    async remove(row){
      this.$confirm(`是否删除${row.name}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          // 删除异步操作,这里需要服务器有res.send返回响应才可以进行下面的执行
          await this.$http.delete(`categories/${row._id}`)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.fetch()
        })
    }
  },
  created() {
    this.fetch();
  },
};
</script>
